<?php
/**
 * 创建推广位【申请】 jd.union.open.position.create
 *
 * See: https://union.jd.com/openplatform/api/655
 * User: Ken.Zhang <kenphp@yeah.net>
 * Date: 2019-05-12
 */
class UnionOpenPositionCreateRequest {

    /**
     * 需要创建的目标联盟id
     */
    private $unionId;

    /**
     * 目标联盟ID对应的授权key，在联盟推广管理页领取
     */
    private $key;

    /**
     * 1：cps推广位 2：cpc推广位
     */
    private $unionType;

    /**
     * 站点类型 1网站推广位2.APP推广位3.社交媒体推广位4.聊天工具推广位5.二维码推广
     */
    private $type;

    /**
     * 推广位名称集合，英文,分割；上限50
     */
    private $spaceNameList;

    /**
     * 站点ID，即网站ID/app ID/snsID ,当type传入4以外的值时，siteId为必填
     */
    private $siteId;

    /**
     * 请求参数
     */
    public $apiParams = [];

    public function setUnionId($union_id)
    {
        $this->unionId = $union_id;
        $this->apiParams['positionReq']['unionId'] = $union_id;
    }

    public function getUnionId()
    {
        return $this->unionId;
    }

    public function setKey($key)
    {
        $this->key = $key;
        $this->apiParams['positionReq']['key'] = $key;
    }

    public function getKey()
    {
        return $this->key;
    }

    public function setUnionType($union_type)
    {
        $this->unionType = $union_type;
        $this->apiParams['positionReq']['unionType'] = $union_type;
    }

    public function getUnionType()
    {
        return $this->unionType;
    }

    public function setType($type)
    {
        $this->type = $type;
        $this->apiParams['positionReq']['type'] = $type;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setSpaceNameList($space_name_list)
    {
        $this->spaceNameList = $space_name_list;
        $this->apiParams['positionReq']['spaceNameList'] = $space_name_list;
    }

    public function getSpaceNameList()
    {
        return $this->spaceNameList;
    }

    public function setSiteId($site_id)
    {
        $this->siteId = $site_id;
        $this->apiParams['positionReq']['siteId'] = $site_id;
    }

    public function getSiteId()
    {
        return $this->siteId;
    }

    public function getApiMethodName()
    {
        return 'jd.union.open.position.create';
    }

    /**
     * 解析结果
     *
     * @param   string      $response
     * @throws  Exception
     * @return  mixed
     */
    public function getResult($response)
    {
        $responseKey = str_replace('.', '_', $this->getApiMethodName()) . '_response';

        $response = @json_decode($response, true);
        if (!array_key_exists($responseKey, $response))
            $resp = $response['errorResponse'];
        else {
            $response[$responseKey]['result'] = json_decode($response[$responseKey]['result']);

            $resp = $response[$responseKey];
        }

        return $resp;
    }

}